<?php

namespace app\modules\backend\controllers;

use app\models\Base;
use app\models\Repair;
use app\models\FormType;
use app\models\FormTag;
use app\models\FormInfo;
use app\tools\Re;
use Yii;
use yii\helpers\Json;

class FormController extends BaseController
{
    /**
     * @param string $type
     * @return array
     */
    public function actionFormTypeList(){
         $page = Yii::$app->request->post('page', 0);
         $name = Yii::$app->request->post('name', '');
         $form_type_query = FormType::find()->where(['status' => 1]);
         if ($name) {
             $form_type_query->andWhere(['like', 'name', $name]);
         }
         $form_type_list = $form_type_query->orderBy("created_at desc")->limit(FormType::PAGE_SIZE)->offset(FormType::PAGE_SIZE*$page)->all();
         $form_type_data = [];
         if ($form_type_list) {
             foreach ($form_type_list as $v) {
                 $form_type_data[] = [
                     'id' => $v->id,
                     'name' => $v->name,
                     'code' => $v->code,
                     'content' => $v->content,
                     'desc' => $v->desc,
                     'status' => $v->status,
                     'created_time' => date("Y-m-d", $v->created_at),
                 ];
             }
         }
         return Re::data([
             'data' => $form_type_data,
             'totalPage' => $form_type_query->count(),
             'currentPage' => $page,
             'perPage' => FormType::PAGE_SIZE
         ]);
    }

    public function actionEditFormType(){
        $id = Yii::$app->request->post('id', 0);
        $post = Yii::$app->request->post();
        return $this->edit(FormType::class, $id, $post);
    }

    public function actionCompanyList(){
        $page = Yii::$app->request->post('page', 0);
        $type = Yii::$app->request->post('type', -1);
        $name = Yii::$app->request->post('name', '');
        $company_query = Repair::find()->where(['status' => [0,1]]);
        if ($type == 1) {
          $company_query->andWhere(['type' => [1,3]]);
        }
        if ($type == 2) {
          $company_query->andWhere(['type' => [2,3]]);
        }
        if ($name) {
            $company_query->andWhere(['like', 'name', $name]);
        }
        $company_lisy = $company_query->orderBy("created_at desc")->limit(Repair::PAGE_SIZE)->offset(Repair::PAGE_SIZE*$page)->all();
        $company_data = [];
        if ($company_lisy) {
            foreach ($company_lisy as $v) {
                $city_value = [];
                if ($v->area_id) {
                  array_push($city_value, $v->area->parentArea->parentArea->id, $v->area->parentArea->id, $v->area->id);
                }
                $company_data[] = [
                    'id' => $v->id,
                    'name' => $v->name,
                    'voltage_level' => $v->voltage_level,
                    'service_type' => $v->service_type,
                    'desc' => $v->desc,
                    'area_name' => $v->area_id ? $v->area->parentArea->parentArea->name.$v->area->parentArea->name.$v->area->name : '',
                    'area_id' => $v->area_id,
                    'city_value' => $city_value,
                    'star_avg' => $v->star_avg,
                    'type' => $v->type,
                    'status' => $v->status,
                    'created_time' => date("Y-m-d", $v->created_at),
                ];
            }
        }
        return Re::data([
            'data' => $company_data,
            'totalPage' => $company_query->count(),
            'currentPage' => $page,
            'perPage' => Repair::PAGE_SIZE
        ]);
    }

    public function actionEditCompany(){
        $id = Yii::$app->request->post('id', 0);
        $post = Yii::$app->request->post();
        return $this->edit(Repair::class, $id, $post);
    }

    public function actionChangeCompanyStatus(){
        $id = Yii::$app->request->post('id', 0);
        $companyInfo = Repair::findOne($id);
        if (empty($companyInfo)) {
            return Re::data(null, '施工单位息读取失败', Re::ERROR);
        }
        $companyInfo->status = $companyInfo->status ? 0 : 1;
        if (!$companyInfo->save()) {
            return Re::data(null, '操作失败', Re::ERROR);
        }
        return Re::data();
    }

    public function actionFormTypeListForSelect(){
      $level = Yii::$app->request->post('level', 0);
      $form_type_query = FormType::find()->where(['status' => 1]);
      if ($level) {
        $form_type_query->andWhere(['level' => $level]);
      }
      $form_type_list = $form_type_query->select('code,name')->all();
      return Re::data(['data' => $form_type_list]);
    }

    public function actionFormList(){
      $page = Yii::$app->request->post('page', 0);
      $form_type = Yii::$app->request->post('form_type', '');
      $name = Yii::$app->request->post('name', '');
      $phone = Yii::$app->request->post('phone', '');
      $company_name = Yii::$app->request->post('company_name', '');
      $form_query = FormInfo::find()->where(['status' => [0,1]]);
      if ($form_type) {
          $form_type_info = FormType::find()->where(['code' => $form_type])->one();
          if ($form_type_info->parent_id) {//二级菜单
            $form_query->andWhere(['form_type' => $form_type]);
          } else {//一级菜单
            $form_type_arr = FormType::find()->where(['parend_id' => $form_type_info->id])->select('code')->column();
            if ($form_type_arr) {
              //$form_types = implode(',', $form_type_arr);
              $form_query->andWhere(['form_type' => $form_type_arr]);
            }
          }

      }
      if ($name) {
          $form_query->andWhere(['like', 'name', $name]);
      }
      if ($phone) {
          $form_query->andWhere(['like', 'phone', $phone]);
      }
      if ($company_name) {
          $form_query->andWhere(['like', 'company_name', $company_name]);
      }
      $form_list = $form_query->orderBy("created_at desc")->limit(FormInfo::PAGE_SIZE)->offset(FormInfo::PAGE_SIZE*$page)->all();
      $form_data = [];
      if ($form_list) {
        foreach ($form_list as $v) {
          $service_name = '';
          if ($v->service_type) {
            $service_type_arr = explode(',', $v->service_type);
            foreach ($service_type_arr as $v1) {
              $formTag = FormTag::findOne($v1);
              $service_name .= $formTag->name.',';
            }
            $service_name = rtrim($service_name, ",");
          }
          $form_data[] = [
            'id' => $v->id,
            'name' => $v->name,
            'phone' => $v->phone,
            'company_name' => $v->company_name,
            'service_type_name' => $service_name,
            'voltage_level_name' => $v->voltage_level ? $v->voltage->name : '',
            'capacity' => $v->capacity,
            'operation_type_name' => $v->operation_type == 1 ? '有人值守' : '无人值守',
            'company_name_sg' => $v->company_sg > 0 ? $v->company->name : $v->company_sg_name,
            'company_name_wt' => $v->company_wt > 0 ? $v->companyWt->name : '',
            'type_name' => $v->form_type ? $v->type->name : '',
            'is_read' => $v->is_read,
            'content' => $v->content,
            'result' => $v->result,
            'content_arr' => $v->content_json ? json_decode($v->content_json) : [],
            'total_money' => $v->total_money,
            'status' => $v->status,
            'created_time' => date("Y-m-d", $v->created_at),
          ];
        }
      }
      return Re::data([
        'data' => $form_data,
        'totalPage' => $form_query->count(),
        'currentPage' => $page,
        'perPage' => FormInfo::PAGE_SIZE
      ]);
    }

    public function actionChangeFormStatus(){
        $id = Yii::$app->request->post('id', 0);
        $status = Yii::$app->request->post('status', 0);
        $form_info = FormInfo::findOne($id);
        if (!$form_info) {
            return Re::data(null, '表单信息读取失败', Re::ERROR);
        }
        $form_info->status = $status;
        $form_info->save();
        return Re::data();
    }

    public function actionReadForm(){
        $id = Yii::$app->request->post('id', 0);
        $content = Yii::$app->request->post('content', '');
        if (empty($id) || empty($content)) {
          return Re::data(null, '参数错误', Re::ERROR);
        }
        $form_info = FormInfo::findOne($id);
        if (!$form_info) {
            return Re::data(null, '表单信息读取失败', Re::ERROR);
        }
        $form_info->result = $content;
        $form_info->is_read = 1;
        $form_info->save();
        return Re::data();
    }

    public function actionCompanyTagListForSelect(){
        $type = Yii::$app->request->post('type', 3);
        $tagList = FormTag::find()->where(['type' => $type, 'status' => 1])->select('id, name')->all();
        return Re::data(['data' => $tagList]);
    }
}
